﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace NNTH
{
    public partial class fLopHoc : DevExpress.XtraEditors.XtraForm
    {
        public fLopHoc()
        {
            InitializeComponent();
        }
        Data.QLTTNNTHEntities db = new Data.QLTTNNTHEntities();
        private void btnThem_Click(object sender, EventArgs e)
        {
            btnLuu.PerformClick();
            if (gridLookUpEdit1.EditValue!=null)
            {
                this.lopHocBindingSource.AddNew();
            }
            else
            {
                MessageBox.Show("Chưa chọn khóa để thêm lớp và lọc danh sách lớp");
            }
           
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            lopHocBindingSource.RemoveCurrent();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
           // MessageBox.Show(errorProvider1.GetError(lopHocGridControl));
            db.SaveChanges();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void fLopHoc_Load(object sender, EventArgs e)
        {
            this.khoaHocBindingSource.DataSource = this.khoaHocBindingSource1.DataSource = db.KhoaHoc;
            this.lopHocBindingSource.DataSource = db.LopHoc;
            giaoVienBindingSource.DataSource = db.GiaoVien;
        }

        private void gridLookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            var a = (string)gridLookUpEdit1.EditValue;
            this.lopHocBindingSource.DataSource = db.LopHoc.Where(p => p.MaKhoaHoc == a);
        }

        private void lopHocBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            var khoa = (string)gridLookUpEdit1.EditValue;
            //var KhoaDT = db.KhoaHoc.Where(p => p.MaKhoaHoc = khoa);
            var stt = db.LopHoc.Where(p => p.MaKhoaHoc == khoa).Count()+1;
            e.NewObject = new Data.LopHoc()
            {
                MaLop = khoa.Trim() + stt.ToString(),
                MaKhoaHoc = khoa.Trim(),
                NgayBD = DateTime.Now,
                NgayKT = DateTime.Now.AddDays(60)
            };
        }
    }
}